import Vue from 'vue'
import VueRouter from 'vue-router'
import local from '@/utils/local'

//解决路由的问题（警告提示）--专门给程序员看的  可以选择不改
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}




//引入element-ui

import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
Vue.use(ElementUI);


Vue.use(VueRouter)

//引入组件(默认指引2个，其他组件使用懒加载（访问地址才会加载组件）)
import Login from '@/views/login/Login.vue'
import Layout from '@/views/layout/Layout.vue'



// import Home from '@/views/home/Home.vue'

// import AccountAdd from '@/views/account/AccountAdd.vue'
// import AccountList from '@/views/account/AccountList.vue'
// import PasswordModify from '@/views/account/PasswordModify.vue'
// import Personal from '@/views/account/Personal.vue'

// import GoodsAdd from '@/views/goods/GoodsAdd.vue'
// import GoodsCate from '@/views/goods/GoodsCate.vue'
// import GoodList from '@/views/goods/GoodsList.vue'

// import Order from '@/views/order/Order.vue'

// import Shop from '@/views/shop/Shop.vue'

// import GoodTotal from '@/views/total/GoodsTotal.vue'
// import OrderTotal from '@/views/total/OrderTotal.vue'



const routes = [
  //登录
  {
    path:'/login',
    component:Login
  },

  //大布局
  /* 所有组件的父亲  可以不用单独配置 */
  {
    path:'/',
    component:Layout,
    redirect:'/home',
    meta:{path:'/home',text:'后台首页'},
    children:[
      {
        //首页
        path:'/home',
        component:()=>import('@/views/home/Home.vue')
        
      }
    ]
  },

  //账户管理
  {
    path:'/account',
    component:Layout,
    redirect:'/account/account-add',
    meta:{path:'/account',text:'账户管理'},
    children:[
      {
        //账号添加
        path:'/account/account-add',
        component:()=>import('@/views/account/AccountAdd.vue'),
        meta:{path:'/account/account-add',text:'账号添加'},
      },
      {
        //账户列表
        path:'/account/account-list',
        component:()=>import('@/views/account/AccountList.vue'),
        meta:{path:'/account/account-list',text:'账户列表'},
      },
      {
        //密码修改
        path:'/account/password-modify',
        component:()=>import('@/views/account/PasswordModify.vue'),
        meta:{path:'/account/password-modify',text:'密码修改'},
      },
      {
        //个人中心
        path:'/account/personal',
        component:()=>import('@/views/account/Personal.vue'),
        meta:{path:'/account/personal',text:'个人中心'},
      }
    ]
  },
  //商品管理
  {
    path:'/goods',
    component:Layout,
    redirect:'/goods/goods-list',
    meta:{path:'/goods',text:'商品管理'},
    children:[
      {
        //商品添加
        path:'/goods/goods-add',
        component:()=>import('@/views/goods/GoodsAdd.vue'),
        meta:{path:'/goods/goods-add',text:'商品添加'},
      },
      {
        //商品分类
        path:'/goods/goods-cate',
        component:()=>import('@/views/goods/GoodsCate.vue'),
        meta:{path:'/goods/goods-cate',text:'商品分类'},
      },
      {
        //商品列表
        path:'/goods/goods-list',
        component:()=>import('@/views/goods/GoodsList.vue'),
        meta:{path:'/goods/goods-list',text:'商品列表'},
      },
     
    ]
  },
   //订单管理
   {
    path:'/order',
    component:Layout,meta:{path:'/order',text:'订单管理'},
    children:[
      {
        //订单管理
        path:'', //订单管理只有一个儿子，可以写空
        component:()=>import('@/views/order/Order.vue')
      },
  
    ]
  },
   //销售统计
   {
    path:'/total',
    component:Layout,
    redirect:'/total/goods-total',
    meta:{path:'/total',text:'销售统计'},
    children:[
      {
        //商品统计
        path:'/total/goods-total', 
        component:()=>import('@/views/total/GoodsTotal.vue'),
        meta:{path:'/total/goods-total',text:'商品统计'},
      },
      {
        //订单统计
        path:'/total/order-total', 
        component:()=>import('@/views/total/OrderTotal.vue'),
        meta:{path:'/total/order-total',text:'订单统计'},
      },
  
    ]
  },
   //店铺管理
   {
    path:'/shop',
    component:Layout,
    meta:{path:'/shop',text:'店铺管理'},
    children:[
      {
        //店铺管理
        path:'', //店铺管理只有一个儿子，可以写空
        component:()=>import('@/views/shop/Shop.vue')
      },
  
    ]
  },
]

const router = new VueRouter({
  routes
})

//全局前置路由守卫
router.beforeEach((to,from,next)=>{
  // console.log(to);
  // console.log(from);
  // console.log(next);
  //判断登录状态
  // token错误
  // let isLogin = window.localStorage.getItem('token') ? true:false
  // console.log(local,1111)
  let isLogin =local.get('token')? true:false

  //登录过
  if(isLogin){
    next()//放行
  }else{
    if(to.path==='/login'){//如果去的登录页面
      next()//放行
    }else{//如果去的是其他页面
      next({path:'/login'})//跳转到登录页面
    }
  }
})

export default router
